#include <bits/stdc++.h>

using namespace std;

int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

void dfs(vector<vector<int>> &graph, vector<vector<bool>> &flag, int x, int y)
{
    for (int i = 0; i < 4; i++)
    {
        int x_pos = x + dir[i][0];
        int y_pos = y + dir[i][1];
        if (x_pos < 0 || x_pos >= (int)graph.size() || y_pos < 0 || y_pos >= (int)graph[0].size())
        {
            continue;
        }
        if (flag[x_pos][y_pos] == false && graph[x_pos][y_pos] == 1)
        {
            flag[x_pos][y_pos] = true;
            dfs(graph, flag, x_pos, y_pos);
        }
    }
}

void bfs(vector<vector<int>> &graph, vector<vector<bool>>& flag, int x, int y)
{
    queue<pair<int, int>> que;
    que.push({x, y});
    flag[x][y] = true;
    while (!que.empty())
    {
        pair<int, int> cur = que.front();
        que.pop();
        for (int i = 0; i < 4; i++)
        {
            int x_pos = cur.first + dir[i][0];
            int y_pos = cur.second + dir[i][1];

            if (x_pos < 0 || x_pos >= (int)graph.size() || y_pos < 0 || y_pos >= (int)graph[0].size())
            {
                continue;
            }

            if (flag[x_pos][y_pos] == false && graph[x_pos][y_pos] == 1)
            {
                que.push({x_pos, y_pos});
                flag[x_pos][y_pos] = true;
            }
        }
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(m, 0));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> graph[i][j];
        }
    }

    int result = 0;
    vector<vector<bool>> flag(n, vector<bool>(m, false));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (flag[i][j] == false && graph[i][j] == 1)
            {
                result++;
                flag[i][j] = true;
                // dfs(graph, flag, i, j);
                bfs(graph, flag, i, j);
            }
        }
    }

    cout << result << endl;
    return 0;
}